using System;
using System.Collections.Generic;
using System.Text;

using System.Xml;

namespace ClientGUI
{
    /// <summary>
    /// A class to give access to information about the configuration of a tag.
    /// Namely:
    /// 1.  What tags can be put under it.
    /// 2.  What attributes can it have.
    /// </summary>
    public class TagConfig
    {
        /// <summary>
        /// The children allowed for this tag configuration
        /// </summary>
        public ReadOnlyCollection<String> AllowedChildren;
        /// <summary>
        /// Allowed attributes of this configuration
        /// </summary>
        public ReadOnlyCollection<String> AllowedAttributes;

        /// <summary>
        /// Constructor
        /// </summary>
        public TagConfig()
        {
            String [] empty = new String [0];
            AllowedAttributes = new ReadOnlyCollection<String>(empty);
            AllowedChildren = new ReadOnlyCollection<String>(empty);
        }

        //TODO:  what about allowed attributes?
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="config"></param>
        public TagConfig(XmlNode config)
        {
            Console.WriteLine(config.InnerXml);
            XmlNodeList children = config.SelectNodes("Children/child");
            if (children != null)
            {
                List<String> allowed_children = new List<string>();
                foreach (XmlNode kid in children)
                {
                    allowed_children.Add(
                        kid.Attributes.GetNamedItem("name").Value);
                }
                AllowedChildren = new ReadOnlyCollection<string>(
                    allowed_children.ToArray());
            }
            else
                AllowedChildren = new ReadOnlyCollection<string>(
                    new String[0]);


            XmlNodeList attrs = 
                config.SelectNodes("Attributes/attribute");
            if (attrs != null)
            {
                List<String> allowed_attr = new List<string>();
                foreach (XmlNode attr in attrs)
                {
                    allowed_attr.Add(
                        attr.Attributes.GetNamedItem("name").Value);
                }
                AllowedAttributes = new ReadOnlyCollection<string>(
                    allowed_attr.ToArray());
            }
        }
    }
}
